#include "messageHandler.h"

message* createMessage(int dataSize)
{
  message* m =  malloc(sizeof(message)); 
  m->type = UNKNOWN;
  m->data = malloc(sizeof(char) * dataSize);
  return m;
}

void freeMessage(message* m)
{
  free(m->data);
  free(m);
}

int getCommand(message* m, char* buffer)
{
  assert(m != NULL);
  assert(buffer != NULL);
  int commandSize = strcspn(buffer, " ");
  DBG("Read command size is %d\n", commandSize);
  char cmd[64];
  cleanBuffer(cmd,64);
  if(commandSize == BUFFER_SIZE)
    {
      m->type = UNKNOWN;
      return 0;
    }
  else
    {
      strncpy(cmd,buffer,commandSize);
      DBG("command read is : %s\n", cmd);
      m->type = getCommandNumber(cmd);
      DBG("Command number is : %d\n", m->type);
      return 1;
    }
}

int getCommandNumber(char* cmd)
{
  if (strstr(cmd,"announce") != NULL)
    return ANNOUNCE;
  else if (strstr(cmd,"look") != NULL)
    return LOOK;
 else if (strstr(cmd,"getfile") != NULL)
    return GETFILE;
 else if (strstr(cmd,"update") != NULL)
    return UPDATE;
 else
   {
     WG("%s","unknown message type\n");
     return UNKNOWN;
   }
}

char* getCommandString(int type)
{
  switch(type)
    {
    case ANNOUNCE:
      return "announce"; break;
    case LOOK:
      return "look"; break;
    case GETFILE:
      return "getfile"; break;
    case UPDATE:
      return "update"; break;
    case UNKNOWN:
      return "unknown"; break;
    default:
      {
	WG("%s","unknown message type\n");
	return NULL;
      }
    }
}

// waiting for 2 ']'
int check_announce(message* m, char* buffer)
{
  DBG("%s","Check for correct data : ANNOUNCE\n");
  char* first = strchr(buffer,']');
  char* end = strrchr(buffer,']');
  if (end != NULL && first != NULL)
    {
      int commandSize = strcspn(buffer, " ") + 1;
      cutString(buffer+commandSize,m->data,end);
      DBG("%s","correct data for ANNOUNCE\n");
      return 1;
    }
  else
    {
      DBG("%s","incorrect data for ANNOUNCE\n");
      return 0;
    }
}

int check_look(message* m, char* buffer)
{
   char* end = strrchr(buffer,']');
   if (end != NULL)
    {
      int commandSize = strcspn(buffer, " ") + 1;
      cutString(buffer+commandSize,m->data,end);
      return 1;
    }
  else
    return 0;
}

int check_getfile(message* m, char* buffer)
{
  char* tmpStr = strdup(buffer);
  char* cmd __attribute__((unused)) = strtok(tmpStr," ") ;
  char* key = strtok(NULL," ");
  char* realKey;
  DBG("key = %s\n", key);
  DBG("keySize = %d\n", strlen(key));
  // User might send a \n at the end of the key, we remove it
  if (strchr(key,'\n') != NULL)
    realKey = strtok(key, "\n");
  else
    realKey = key;
  DBG("realkeySize = %d\n", strlen(realKey));
  unsigned int KEY_SIZE = 5;
  if (strlen(realKey) == KEY_SIZE)
    {
      strncpy(m->data,realKey,KEY_SIZE);
      free(tmpStr);
      return 1;
    }
  else if (strlen(realKey) > KEY_SIZE)
    {
      free(tmpStr);
      return 2;
    }
  else
    {
      free(tmpStr);
      return 0;
    }
}
 
int check_update(message* m, char* buffer)
{
  char* first = strchr(buffer,']');
  char* end = strrchr(buffer,']');
  if (end != NULL && first != NULL)
    {
      int commandSize = strcspn(buffer, " ") + 1;
      cutString(buffer+commandSize,m->data,end);
      return 1;
    }
  else
    return 0;
}


